#include "CapitalMarketFunds.h"
#include "../FIDataCenter.h"
#include "CapitalMarketSettings.h"
#include "../StringResource.h"
#include <core/time/time.h>
#include <core/StyleMgr.h>
#include <uam/ServerTimeMgr.h>
#include <widgets/SImageButton.h>
#include <widgets/table/STable.h>
#include <widgets/table/STableColumnSet.h>
#include <QVBoxLayout>
#include <QLabel>
#include <QColor>
#include <QPainter>
#include <QScrollBar>
#include <QTableView>
#include <QHeaderView>
#include <uam/UserAccountManager.h>

namespace
{
#define ALIGN_LEFT  (Qt::Alignment)(Qt::AlignVCenter | Qt::AlignLeft)
#define ALIGN_RIGHT  (Qt::Alignment)(Qt::AlignVCenter | Qt::AlignRight)

int CurrentDate() {
    time_t tToday = ServerTimeMgr::instance().serverTime();
    tm tmToday = qb::base::time2tm(tToday);

    return (tmToday.tm_year + 1900) * 10000 + (tmToday.tm_mon + 1) * 100 + tmToday.tm_mday;
}

QPixmap drawTextWithBorder(const QString &text, const QFont &font, const QColor &textColor, const QColor &borderColor, int borderWidth) {
    QFontMetrics metrics(font);
    int textWidth = metrics.horizontalAdvance(text);
    int textHeight = metrics.height();

    QPixmap pixmap(textWidth + 2 * (borderWidth + 2), textHeight + 2 * borderWidth);
    pixmap.fill(Qt::transparent);

    QPainter painter(&pixmap);
    painter.setFont(font);
    painter.setPen(textColor);

    // Draw the text
    painter.drawText( borderWidth+2, metrics.ascent() + borderWidth+1, text);

    // Set the border
    painter.setPen(borderColor);
    painter.drawRect( borderWidth, borderWidth, textWidth +borderWidth+2, textHeight);

    return pixmap;
}
}

class MMVirtualTable : public SVirtualTable
{
public:
    MMVirtualTable(QWidget* p):SVirtualTable(p) {}
protected:
    void resizeEvent(QResizeEvent* event) override {
        // we override resizeEvent to let it do nothing
    }
    bool eventFilter(QObject* watched, QEvent* event) override {
        // we override eventFilter to let it do nothing
        return false;
    }
};

CapitalMarketFunds::CapitalMarketFunds(QString strTitle, QWidget* parent)
	: SDialog(parent)
{
	setTitle(strTitle);
    setEnableSetting(true);

    /*QHBoxLayout* titleLayout = qobject_cast<QHBoxLayout*>(m_title->layout());
	if (titleLayout)
	{
        m_lblUpdateTime = new QLabel("", this);
        m_lblUpdateTime->show();
        titleLayout->insertWidget(3, m_lblUpdateTime);

        QString styleSheet = QString("color:#%1")
            .arg(StyleMgr::instance().color("SupportingWord").rgb(), 0, 16);
        m_lblUpdateTime->setStyleSheet(styleSheet);
        QFont fn = m_title->font();
        fn.setPixelSize(11);
        m_lblUpdateTime->setFont(fn);
    }*/

	QLayout* layout = this->layout();

    m_list = new MMVirtualTable(this);
	m_model = new DefaultTableModel(this);
	m_model->setTable(m_list);
	layout->addWidget(m_list);

	m_list->setHeaderSortable(true);
	m_list->table()->setModel(m_model);
	m_list->table()->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
	m_list->table()->horizontalHeader()->setSectionsMovable(true);
	m_list->table()->horizontalHeader()->show();
	m_list->table()->setContextMenuPolicy(Qt::CustomContextMenu);

    // use the native scroll bar of QTableView
    m_list->table()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);

    connect(&FIDataCenter::instance(), &FIDataCenter::recvCIBRepo, this, &CapitalMarketFunds::onCIBRepoDataArrived);
    connect(&FIDataCenter::instance(), &FIDataCenter::recvExchRepo, this, &CapitalMarketFunds::onExchRepoDataArrived);
    //connect(&FIDataCenter::instance(), &FIDataCenter::recvIRS, this, &CapitalMarketFunds::onIRSDataArrived);
    connect(&FIDataCenter::instance(), &FIDataCenter::recvShibor, this, &CapitalMarketFunds::onShiborDataArrived);
    connect(&FIDataCenter::instance(), &FIDataCenter::recvRepoFixing, this, &CapitalMarketFunds::onRepoFixingDataArrived);
    connect(&FIDataCenter::instance(), &FIDataCenter::recvCIBRepoFixing, this, &CapitalMarketFunds::onCIBRepoFixingDataArrived);

    connect(&CapitalMarketSettings::instance(CapitalMarketSettings::Funds), &CapitalMarketSettings::orderChanged, this, &CapitalMarketFunds::onOrderChanged );

    QFont f_postfix = font();
    f_postfix.setPixelSize(10);
    m_expire = drawTextWithBorder("昨", f_postfix, QColor(94,94,94), QColor(94,94,94), 1);

	initTable();
}

void CapitalMarketFunds::onOrderChanged()
{
    initModel();
    updateTable();
}

void CapitalMarketFunds::initModel()
{
    QFont f = font();
    f.setPixelSize(13);
    f.setBold(true);

    m_model->setRowCount(0);
    const std::vector<int>& current_order = CapitalMarketSettings::instance(CapitalMarketSettings::Funds).getOrder();
    m_model->setRowCount(current_order.size());

    int row = 0;
    for(const auto& id : current_order) {
        QStandardItem* item = new QStandardItem();
        item->setData(f, Qt::FontRole);
        //QString idStr = QString::fromLocal8Bit(o.c_str());
        item->setData(id, DataIdRole);
        item->setData(CapitalMarketSettings::getName(id), Qt::DisplayRole);
        item->setData(StyleMgr::instance().color("TextNormal"), Qt::ForegroundRole);
        item->setData((int)(ALIGN_LEFT), Qt::TextAlignmentRole);

        m_model->setItem(row++, COL_TERM, item);
    }
}

void CapitalMarketFunds::initTable()
{
	m_tableSet = new STableColumnSet(m_list, "", this);

	BEGIN_COLUMN;
	ADD_COLUMN(COL_TERM, m_tableSet, NewStringTable::strMoneyMarketListHead(COL_TERM), 0, 60, 20, 0.33, false, ALIGN_LEFT, kAuto, "");
	ADD_COLUMN(COL_RATE, m_tableSet, NewStringTable::strMoneyMarketListHead(COL_RATE), 0, 60, 20, 0.33, false, ALIGN_RIGHT, kAuto, "");
	ADD_COLUMN(COL_BP, m_tableSet, NewStringTable::strMoneyMarketListHead(COL_BP), 0, 60, 20, 0.34, false, ALIGN_RIGHT, kAuto, "");
	m_tableSet->apply();

    initModel();

    FIDataCenter::instance().reqCIBRepo();
    FIDataCenter::instance().reqExchRepo();
    FIDataCenter::instance().reqIRS();
    FIDataCenter::instance().reqShibor();
    FIDataCenter::instance().reqRepoFixing();
    FIDataCenter::instance().reqCIBRepoFixing();

    updateTable();
}

void CapitalMarketFunds::updateTable(bool reset)
{
    onCIBRepoDataArrived(true);
    onExchRepoDataArrived(true);
    onIRSDataArrived(true);
    onShiborDataArrived(true);
    onRepoFixingDataArrived(true);
    onCIBRepoFixingDataArrived(true);
}

void CapitalMarketFunds::onCIBRepoDataArrived(bool dataChange)
{
    QFont f = font();
    f.setPixelSize(13);
    f.setBold(true);

    SCOPED_MUTEX(FIDataCenter::instance().m_dataCIBRepo.getLock());
    const FICIBRepoData::MAP_DATA& data = FIDataCenter::instance().m_dataCIBRepo.getData();

    const int colCount = m_model->columnCount();

    for (int row = 0; row < m_model->rowCount(); ++row) {
        QStandardItem* item_term = m_model->item(row, COL_TERM);
        if (item_term == nullptr)
            break; // assert false

        const std::string& term = item_term->data(Qt::DisplayRole).toString().toStdString();
        const auto iter = data.find(term);
        if (iter == data.end()) {
            continue;
        }
        const xQbCibRepo_c& unit = iter->second;
        QColor clrBP;
        QString strText;

        for (int col = 1; col < colCount; ++col)
        {
            //if (!m_tableSet->isColVisible(col))continue;
            QStandardItem* item = m_model->item(row, col);
            if (item == nullptr) {
                item = new QStandardItem();
                item->setData(f, Qt::FontRole);
                //QString idStr = QString::fromLocal8Bit(unit.m_szID);
                //item->setData(idStr, DataIdRole);
            }
            // item->clearData();

            switch (col)
            {
            /*case COL_TERM:
                strText = QString::fromLocal8Bit(unit.m_szID);
                item->setData(strText, Qt::DisplayRole);
                item->setData(StyleMgr::instance().color("TextNormal"), Qt::ForegroundRole);
                break;*/
            case COL_RATE:
                strText = QString::asprintf("%.04f", unit.m_dRate);
                item->setData(strText, Qt::DisplayRole);
                item->setData(StyleMgr::instance().color("Highlight"), Qt::ForegroundRole);
                item->setData((int)(ALIGN_RIGHT), Qt::TextAlignmentRole);
                break;
            case COL_BP:
                if ((unit.m_dDiff > 1e-6))
                {
                    strText = QString::asprintf("+%.02f", unit.m_dDiff);
                    clrBP = StyleMgr::instance().color("Down_Reduce_H1");
                }
                else if (unit.m_dDiff < -1e-6)
                {
                    strText = QString::asprintf("%.02f", unit.m_dDiff);
                    clrBP = StyleMgr::instance().color("Up_Increase_H1");
                }
                else
                {
                    strText = "0.00";
                    clrBP = StyleMgr::instance().color("Highlight");
                }
                item->setData(strText, Qt::DisplayRole);
                item->setData(clrBP, Qt::ForegroundRole);
                item->setData((int)(ALIGN_RIGHT), Qt::TextAlignmentRole);
                break;
            default:break;
            }
            m_model->setItem(row, col, item);
        }
    }
}

static bool isAuthValid(QBExchangeRepoBond* pRepo)
{
    return SSUserAccountManager::instance().HasExchangeAuth(pRepo->GetListedMarket());
}

void CapitalMarketFunds::onExchRepoDataArrived(bool dataChange)
{
    QFont f = font();
    f.setPixelSize(13);
    f.setBold(true);

    SCOPED_MUTEX(FIDataCenter::instance().m_dataExchRepo.getLock());
    const QBExchangeRepoBondContainer &container = FIDataCenter::instance().m_dataExchRepo.getContainer();

    const int colCount = m_model->columnCount();

    QString strText;
    QColor clrBP;

    for (int row = 0; row < m_model->rowCount(); ++row) {
        QStandardItem* item_term = m_model->item(row, COL_TERM);
        if (item_term == nullptr)
            break; // assert false

        const std::string& term = item_term->data(Qt::DisplayRole).toString().toStdString();
        QBExchangeRepoBond* pRepo = container.GetRepoBondByCode(term.c_str());
        if (nullptr == pRepo)
            continue;

        const bool repoValid = isAuthValid(pRepo);

        for (int col = 1; col < colCount; ++col) {
            //if (!m_tableSet->isColVisible(col))continue;
            QStandardItem* item = m_model->item(row, col);
            if (item == nullptr) {
                item = new QStandardItem();
                item->setData(f, Qt::FontRole);
                //QString idStr = QString::fromLocal8Bit(term.c_str());
                //item->setData(idStr, DataIdRole);
            }

            switch (col)
            {
            /*case COL_TERM:
                strText = QString::fromLocal8Bit(sId.c_str());
                item->setData(strText, Qt::DisplayRole);
                item->setData(StyleMgr::instance().color("TextNormal"), Qt::ForegroundRole);
                break;*/
            case COL_RATE:
                if (repoValid) {
                    double fPrice = atof(pRepo->GetLastPrice());
                    if (pRepo && strlen(pRepo->GetLastPrice()) > 0)
                        strText = QString::asprintf("%.04f", fPrice);
                    else
                        strText = "--";
                } else
                    strText = "--";
                item->setData(strText, Qt::DisplayRole);
                item->setData(StyleMgr::instance().color("Highlight"), Qt::ForegroundRole);
                item->setData((int)(ALIGN_RIGHT), Qt::TextAlignmentRole);
                break;
            case COL_BP:
            {
                bool bValid = false;
                do {
                    if (repoValid && (strlen(pRepo->GetPreClosePrice()) > 0 || strlen(pRepo->GetLastPrice()) > 0))
                    {
                        double fPrice = atof(pRepo->GetLastPrice());
                        fPrice -= atof(pRepo->GetPreClosePrice());

                        if (strcmp(pRepo->GetPreClosePrice(), pRepo->GetLastPrice()) == 0) break;
                        else if (fPrice > 1e-6)
                        {
                            bValid = true;
                            strText = QString::asprintf("+%.02f", fPrice * 100.00);
                            clrBP = StyleMgr::instance().color("Down_Reduce_H1");

                        }
                        else if (fPrice < -1e-6)
                        {
                            bValid = true;
                            strText = QString::asprintf("%.02f", fPrice * 100.00);
                            clrBP = StyleMgr::instance().color("Up_Increase_H1");
                        }
                    }
                } while (0);
                if (!bValid)
                {
                    strText = "0.00";
                    clrBP = StyleMgr::instance().color("Highlight");
                }
                item->setData(strText, Qt::DisplayRole);
                item->setData(clrBP, Qt::ForegroundRole);
                item->setData((int)(ALIGN_RIGHT), Qt::TextAlignmentRole);
                break;
            }
            default:break;
            }
            m_model->setItem(row, col, item);
        }
    }

    if(dataChange)
    {
        const std::list<std::string>& lstFlash = FIDataCenter::instance().m_dataExchRepo.getFlash();
        if (lstFlash.size() <= 0)return;
        for (auto& it : lstFlash)
        {
            FlashInfo fi;
            fi.entireRow = true;
            fi.id = QString::fromLocal8Bit(it.c_str());
            m_list->addFlashInfo(fi, false);
        }
        m_list->startFlash();
    }
}

void CapitalMarketFunds::onIRSDataArrived(bool dataChange)
{

}

void CapitalMarketFunds::onShiborDataArrived(bool dataChange)
{
    const char kShiborTag[] = "Shi ";
    QFont f = font();
    f.setPixelSize(13);
    f.setBold(true);

    SCOPED_MUTEX(FIDataCenter::instance().m_dataShibor.getLock());
    const xFixingRepoRateList_c& data = FIDataCenter::instance().m_dataShibor.getData();

    QString strText;
    QColor clrBP;

    std::vector<std::string> vctOrder;

    const int nToday = CurrentDate();

    const int colCount = m_model->columnCount();
    for (int row = 0; row < m_model->rowCount(); ++row) {

        QStandardItem* item_term = m_model->item(row, COL_TERM);
        if (item_term == nullptr)
            break; // assert false

        const QString& q_term = item_term->data(Qt::DisplayRole).toString();
        if (!q_term.startsWith(kShiborTag))
            continue;
        const std::string term = q_term.toStdString();
        const std::string s_term = q_term.mid(sizeof(kShiborTag)-1).toStdString();

        auto itr = std::find_if(data.m_List.begin(), data.m_List.end(), [&s_term](const xBaseUnit_c& x){
            return s_term == x.m_term;
        });

        if (itr == data.m_List.end())
            continue;

        vctOrder.push_back(term);

        const xBaseUnit_c* pbu = &(*itr);

        if (QStandardItem* item = m_model->item(row, COL_TERM)) {
            if ((int)pbu->m_date < nToday) {
                item->setData(m_expire, PostfixImgRole);
            } else {
                item->setData(QVariant{}, PostfixImgRole); // clear
            }
        }

        //QString idStr = QString::fromLocal8Bit(term.c_str());
        for (int col = 1; col < colCount; ++col)
        {
            //if (!m_tableSet->isColVisible(col))continue;
            QStandardItem* item = m_model->item(row, col);
            if (item == nullptr)
                item = new QStandardItem();
            item->clearData();
            item->setData(f, Qt::FontRole);
            //item->setData(idStr, DataIdRole);
            switch (col)
            {
            case COL_RATE:
            {
                if (pbu && abs(pbu->m_rate) > 1e-6)
                {
                    strText = QString::asprintf("%.04f", pbu->m_rate * 100.00);
                }
                else
                {
                    strText = "0.00";
                }
                item->setData(strText, Qt::DisplayRole);
                item->setData(StyleMgr::instance().color("Highlight"), Qt::ForegroundRole);
                item->setData((int)(ALIGN_RIGHT), Qt::TextAlignmentRole);
                break;
            }
            case COL_BP:
            {
                if (pbu)
                {
                    if (pbu->m_change > 1e-6)
                    {
                        strText = QString::asprintf("+%.02f", pbu->m_change * 10000.00);
                        clrBP = StyleMgr::instance().color("Down_Reduce_H1");
                    }
                    else if (pbu->m_change < -1e-6)
                    {
                        strText = QString::asprintf("%.02f", pbu->m_change * 10000.00);
                        clrBP = StyleMgr::instance().color("Up_Increase_H1");
                    }
                    else
                    {
                        strText = "0.00";
                        clrBP = StyleMgr::instance().color("Highlight");
                    }
                }
                else
                {
                    strText = "0.00";
                    clrBP = StyleMgr::instance().color("Highlight");
                }
                item->setData(strText, Qt::DisplayRole);
                item->setData(clrBP, Qt::ForegroundRole);
                item->setData((int)(ALIGN_RIGHT), Qt::TextAlignmentRole);
                break;
            }
            default:break;
            }
            m_model->setItem(row, col, item);
        }
    }

    if (dataChange) {
        for (auto& it : vctOrder)
        {
            FlashInfo fi;
            fi.entireRow = true;
            fi.id = QString::fromLocal8Bit(it.c_str());
            m_list->addFlashInfo(fi, false);
        }
        m_list->startFlash();
    }
}

void CapitalMarketFunds::onRepoFixingDataArrived(bool dataChange)
{
    QFont f = font();
    f.setPixelSize(13);
    f.setBold(true);

    SCOPED_MUTEX(FIDataCenter::instance().m_dataRepoFixing.getLock());
    const xFixingRepoRateList_c& data = FIDataCenter::instance().m_dataRepoFixing.getData();

    std::vector<std::string> vctOrder;
    const int nToday = CurrentDate();
    const int colCount = m_model->columnCount();
    QString strText;
    QColor clrBP;
    for (int row=0; row<m_model->rowCount(); ++row) {
        QStandardItem* item_term = m_model->item(row, COL_TERM);
        if (item_term == nullptr)
            break; // assert false

        const std::string& term = item_term->data(Qt::DisplayRole).toString().toStdString();
        auto itr = std::find_if(data.m_List.begin(), data.m_List.end(), [&term](const xBaseUnit_c& x){
            return term == x.m_term;
        });

        if (itr == data.m_List.end())
            continue;

        vctOrder.push_back(term);

        const xBaseUnit_c& bu = *itr;

        if (QStandardItem* item = m_model->item(row, COL_TERM)) {
            if ((int)bu.m_date < nToday) {
                item->setData(m_expire, PostfixImgRole);
            } else {
                item->setData(QVariant{}, PostfixImgRole); // clear
            }
        }

        //QString idStr = QString::fromLocal8Bit(term.c_str());
        for (int col = 1; col < colCount; ++col)
        {
            //if (!m_tableSet->isColVisible(col))continue;
            QStandardItem* item = m_model->item(row, col);
            if (item == nullptr)
                item = new QStandardItem();
            item->clearData();
            item->setData(f, Qt::FontRole);
            //item->setData(idStr, DataIdRole);
            switch (col)
            {
            /*case COL_TERM:
                strText = QString::fromLocal8Bit(sId.c_str());
                item->setData(strText, Qt::DisplayRole);
                item->setData(StyleMgr::instance().color("TextNormal"), Qt::ForegroundRole);
                break;*/
            case COL_RATE:
            {
                if (abs(bu.m_rate) > 1e-6)
                {
                    strText = QString::asprintf("%.04f", bu.m_rate * 100.00);
                }
                else
                {
                    strText = "0.00";
                }
                item->setData(strText, Qt::DisplayRole);
                item->setData(StyleMgr::instance().color("Highlight"), Qt::ForegroundRole);
                item->setData((int)(ALIGN_RIGHT), Qt::TextAlignmentRole);
                break;
            }
            case COL_BP:
            {
                if (bu.m_change > 1e-6)
                {
                    strText = QString::asprintf("+%.02f", bu.m_change * 10000.00);
                    clrBP = StyleMgr::instance().color("Down_Reduce_H1");
                }
                else if (bu.m_change < -1e-6)
                {
                    strText = QString::asprintf("%.02f", bu.m_change * 10000.00);
                    clrBP = StyleMgr::instance().color("Up_Increase_H1");
                }
                else
                {
                    strText = "0.00";
                    clrBP = StyleMgr::instance().color("Highlight");
                }
                item->setData(strText, Qt::DisplayRole);
                item->setData(clrBP, Qt::ForegroundRole);
                item->setData((int)(ALIGN_RIGHT), Qt::TextAlignmentRole);
                break;
            }
            default:break;
            }
            m_model->setItem(row, col, item);
        }
    }

    if (dataChange)  {
        for (auto& it : vctOrder)
        {
            FlashInfo fi;
            fi.entireRow = true;
            fi.id = QString::fromLocal8Bit(it.c_str());
            m_list->addFlashInfo(fi, false);
        }
        m_list->startFlash();
    }
}

void CapitalMarketFunds::onCIBRepoFixingDataArrived(bool dataChange)
{
    QFont f = font();
    f.setPixelSize(13);
    f.setBold(true);

    SCOPED_MUTEX(FIDataCenter::instance().m_dataCIBRepoFixing.getLock());
    const xFixingDepoRepoRateList_c& data = FIDataCenter::instance().m_dataCIBRepoFixing.getData();

    std::vector<std::string> vctOrder;
    const int nToday = CurrentDate();
    const int colCount = m_model->columnCount();
    QString strText;
    QColor clrBP;
    for (int row=0; row<m_model->rowCount(); ++row)
    {
        QStandardItem* item_term = m_model->item(row, COL_TERM);
        if (item_term == nullptr)
            break; // assert false

        const std::string& term = item_term->data(Qt::DisplayRole).toString().toStdString();
        auto itr = std::find_if(data.m_List.begin(), data.m_List.end(), [&term](const xBaseUnit_c& x){
            return term == x.m_term;
        });

        if (itr == data.m_List.end())
            continue;

        vctOrder.push_back(term);

        const xBaseUnit_c& bu = *itr;
        if (QStandardItem* item = m_model->item(row, COL_TERM)) {
            if ((int)bu.m_date < nToday) {
                item->setData(m_expire, PostfixImgRole);
            } else {
                item->setData(QVariant{}, PostfixImgRole); // clear
            }
        }

        //QString idStr = QString::fromLocal8Bit(term.c_str());
        for (int col = 1; col < colCount; ++col)
        {
            //if (!m_tableSet->isColVisible(col))continue;
            QStandardItem* item = m_model->item(row, col);
            if (item == nullptr)
                item = new QStandardItem();
            item->clearData();
            item->setData(f, Qt::FontRole);
            //item->setData(idStr, DataIdRole);
            switch (col)
            {
            /*case COL_TERM:
                strText = QString::fromLocal8Bit(sId.c_str());
                item->setData(strText, Qt::DisplayRole);
                item->setData(StyleMgr::instance().color("TextNormal"), Qt::ForegroundRole);
                break;*/
            case COL_RATE:
            {
                if (abs(bu.m_rate) > 1e-6)
                {
                    strText = QString::asprintf("%.04f", bu.m_rate * 100.00);
                }
                else
                {
                    strText = "0.00";
                }
                item->setData(strText, Qt::DisplayRole);
                item->setData(StyleMgr::instance().color("Highlight"), Qt::ForegroundRole);
                item->setData((int)(ALIGN_RIGHT), Qt::TextAlignmentRole);
                break;
            }
            case COL_BP:
            {
                if (bu.m_change > 1e-6)
                {
                    strText = QString::asprintf("+%.02f", bu.m_change * 10000.00);
                    clrBP = StyleMgr::instance().color("Down_Reduce_H1");
                }
                else if (bu.m_change < -1e-6)
                {
                    strText = QString::asprintf("%.02f", bu.m_change * 10000.00);
                    clrBP = StyleMgr::instance().color("Up_Increase_H1");
                }
                else
                {
                    strText = "0.00";
                    clrBP = StyleMgr::instance().color("Highlight");
                }
                item->setData(strText, Qt::DisplayRole);
                item->setData(clrBP, Qt::ForegroundRole);
                item->setData((int)(ALIGN_RIGHT), Qt::TextAlignmentRole);
                break;
            }
            default:break;
            }
            m_model->setItem(row, col, item);
        }
    }

    if (dataChange)
    {
        for (auto& it : vctOrder)
        {
            FlashInfo fi;
            fi.entireRow = true;
            fi.id = QString::fromLocal8Bit(it.c_str());
            m_list->addFlashInfo(fi, false);
        }
        m_list->startFlash();
    }

}

SDialog* CapitalMarketFunds::clone(QWidget* parent)
{
    CapitalMarketFunds* cloned = new CapitalMarketFunds(m_strTitle, parent);
    SImageButton* a =   this->findChild<SImageButton*>(SDialogTitle::kbtnSettingObjName);
    SImageButton* b = cloned->findChild<SImageButton*>(SDialogTitle::kbtnSettingObjName);
    if (nullptr != a && nullptr != b) {
        connect(b, &SImageButton::clicked, a, &SImageButton::clicked);
    }
    return cloned;
}

